//
//  ShopPanel.cpp
//  petgame
//
//  Created by zz on 2018/12/13.
//

#include "ShopPanel.hpp"


ShopPanel*ShopPanel::create(){
//    创建商店对象
    auto shoppanel=new ShopPanel();
//    初始化商店对象
    if(shoppanel&&shoppanel->init()){
        
//        自动释放对象
        shoppanel->autorelease();
        return shoppanel;
    }else{
        
//        安全删除对象
        CC_SAFE_DELETE(shoppanel);
        return NULL;
    }
}
bool ShopPanel::init(){
    
//    父类初始化
    if(!Layer::init()){
    
        return false;
    }
//    创建背景图片
    auto back=Sprite::create("backshop.png");
//    设置背景图片坐标
    back->setPosition(this->getContentSize()/2);
    back->setScale(0.45);
//    加入到场景渲染中
    this->addChild(back);
    
//    创建列表对象
    auto listView=ListView::create();
//    设置列表尺寸
    listView->setContentSize(Size(700,260));
//    把列表对象添加到渲染
    this->addChild(listView);
    listView->setPosition(Point(-215,160));
//    裁剪多出的区域
    listView->setClippingEnabled(true);
//    设置列表的显示方向
    listView->setDirection(ListView::Direction::VERTICAL);
    
//    商品图片资源数据集合
    string shopitemlist[16]={
        "f1.png",
        "f2.png",
        "f3.png",
        "f4.png",
        "f5.png",
        "f6.png",
        "f7.png",
        "f1.png",
        "f2.png",
        "f3.png",
        "f4.png",
        "f5.png",
        "f6.png",
        "f7.png",
        "f1.png",
        "f1.png"
    };
//    商品名称集合
    string shopitemnameList[16]={
      
        "甜甜圈",
        "夹心饼干",
        "糖果",
        "香蕉",
        "三明治",
        "玉米",
        "虾仁",
        "甜甜圈",
        "夹心饼干",
        "糖果",
        "香蕉",
        "三明治",
        "玉米",
        "虾仁",
    };
//    商品说明集合
    string shopitemIntr[16]={
        
        "饥饿值-10",
         "饥饿值-10",
         "饥饿值-10",
         "饥饿值-30",
         "饥饿值-10",
         "饥饿值-20",
         "饥饿值-10",
         "饥饿值-10",
         "饥饿值-10",
         "饥饿值-20",
        "饥饿值-10",
         "饥饿值-10",
         "饥饿值-30",
         "饥饿值-10",
    };
//    商品效果集合
    int effectlist[16]={
        
        10,
        10,
        10,
        30,
        10,
        20,
        10,
        10,
        10,
        20,
        10,
        10,
        30,
        10
    };
//    商品价格集合
    int priceList[16]={
        
        120,
        100,
        80,
        120,
        140,
        150,
        200,
        220,
        120,
        70,
        130,
        168,
        220,
        140
    };
    
    
    
//    创建商品
    for(int i=0;i<=13;i++){
//        新建widget对象
    auto wi=Widget::create();
//        设置Widget尺寸
    wi->setContentSize(Size(400,56));
//        设置widget锚点
    wi->setAnchorPoint(Point(0,0));
//        创建商品对象
    auto bh=ShopItem::create(shopitemlist[i],shopitemnameList[i],shopitemIntr[i],Point(0,0), priceList[i],effectlist[i]);
    wi->addChild(bh);
//        添加到商品列表中
    listView->pushBackCustomItem(wi);
    
}
//    创建返回按钮
    auto backbtn=MenuItemImage::create("backd.png","backds.png",CC_CALLBACK_1(ShopPanel::backFun,this));
//    设置按钮位置
    backbtn->setPosition(Vec2(this->getContentSize().width/2,135));
    backbtn->setScale(0.5);
    
//    创建菜单容器
    auto menu=Menu::create(backbtn,NULL);
//    设置容器位置
    menu->setPosition(Point::ZERO);
//    把菜单加入到渲染
    this->addChild(menu);
    
    
    this->_diamond=LabelTTF::create("40","Airal",10);
    this->_diamond->setPosition(Vec2(65,448));
    this->_diamond->setAnchorPoint(Point(0.5,0.5));
    this->addChild(this->_diamond);
    
//    创建金币数量文本显示对象
    this->_gold=LabelTTF::create(__String::createWithFormat("%i",ShopManager::getInstance()->getGlod())->getCString(),"Arial", 10);
//    设置文本b对象锚点
    this->_gold->setAnchorPoint(Point(0.5,0.5));
//    设置文本对象坐标
    this->_gold->setPosition(Point(298,448));
//    加入到渲染
    this->addChild(this->_gold);
//    设置文本对象标记，以便在后面能根据标记快速获取对象
    this->_gold->setTag(999);

    return true;
}

void ShopPanel::backFun(Ref*pSender){
//    关闭商店
    this->removeFromParent();
}

